package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.entity.AddressBook;
import com.sky.exception.AddressBookBusinessException;
import com.sky.result.Result;
import com.sky.service.addressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "地址簿相关接口")
@RequestMapping("/user/addressBook")
public class addressBookController {

    @Autowired
    private addressBookService addressBookService;

    @ApiOperation("新增地址")
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        log.info("新增地址:{}", addressBook);
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }

    @ApiOperation("查询当前登录用户的所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> listAddressBook() {
        log.info("查询当前登录用户的所有地址信息");
        List<AddressBook> addressBook =  addressBookService.list();
        return Result.success(addressBook);
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> defaultAddressBook() {
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.getDefault();
        if (addressBook == null) return Result.error("没有查询到默认地址");
        return Result.success(addressBook);
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result deleteAddressBook(Long id) {
        log.info("根据id删除地址:{}",id);
        addressBookService.deleteAddressBook(id);
        return Result.success();
    }

    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id) {
        log.info("根据id查询地址:{}",id);
        return Result.success(addressBookService.getAddressBookById(id));
    }

    @ApiOperation("根据Id修改地址")
    @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook) {
        log.info("根据id修改地址:{}",addressBook);
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result updateDefaultAddressBook(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址:{}",addressBook);
        addressBookService.updateDefaultAddressBook(addressBook);
        return Result.success();
    }

}
